var thisUser;
$(document).ready(function(){
	checkLoginStatus();
});
function getRealPath(){
  //获取当前网址，如： http://localhost:8083/myproj/view/my.jsp
   var curWwwPath=window.document.location.href;
   //获取主机地址之后的目录，如： myproj/view/my.jsp
  var pathName=window.document.location.pathname;
  var pos=curWwwPath.indexOf(pathName);
  //获取主机地址，如： http://localhost:8083
  var localhostPaht=curWwwPath.substring(0,pos);
  //获取带"/"的项目名，如：/myproj
  var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
 
 //得到了 http://localhost:8083/myproj
  var realPath=localhostPaht+projectName;
  return realPath;
}
//通用设置cookie
function setCookie(name,value,day){
	if(day){
		var date=new Date(); 
		date.setTime(date.getTime()+day*24*3600*1000); 
		document.cookie = name + "="+ escape(value) + ";expires=" + date.toGMTString()+";path=/";
	}else{
		var date=new Date(); 
		date.setTime(date.getTime()+7*24*3600*1000); 
		document.cookie = name + "="+ escape(value) + ";expires=" + date.toGMTString()+";path=/";
	}
}
//通用读取cookie
function getCookie(name){
	var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
	if(arr=document.cookie.match(reg)){
        return unescape(arr[2]); 
	}
    else 
        return null; 
}
//通用删除cookie
function delCookie(name){
    var date=new Date(); 
    date.setTime(date.getTime()-1); 
    var cval=getCookie(name); 
    if(cval!=null) 
        document.cookie = name + "="+cval+";expires="+date.toGMTString()+";path=/"; 
}
//退出登录
function logout(){
	delCookie("user");
	window.location.href = getRealPath()+"/views/user/login.html";
}
//载入网页检查cookie
function checkLoginStatus(){
	if(getCookie("user")!=null){
		thisUser = JSON.parse(getCookie("user"));
		$("li#userbox a").remove();
		$("#userbox").append('<a href="javascript:void(0);"><img src="'+getRealPath()+JSON.parse(getCookie("user")).head+'"></a>');
		$("#userbox").append('<ul id="menu"></ul>');
		$("#menu").append('<li><a href="'+getRealPath()+'/views/blog/myblog.html">我的博客</a></li>');
		$("#menu").append('<li><a href="'+getRealPath()+'/views/market/market.html">商城</a></li>');
		$("#menu").append('<li><a href="javascript:void(0)" onclick="logout()">退出</a></li>');
		displayUserMenu();
	}else{
		return;
	}
}
//hover头像显示用户菜单
function displayUserMenu(){
	$("#userbox").hover(function(){
		$("#menu").show();
	},function(){
		$("#menu").hide();
	});
}
//退出登录
function logout(){
	delCookie("user");
	location.href = getRealPath()+"/views/user/login.html";
}
////屏蔽特殊字符输入
//function showKeyPress(evt) {
//	evt = (evt) ? evt : window.event;
//	return checkSpecificKey(evt.keyCode);
//}
//function checkSpecificKey(keyCode) {
//	var specialKey = " [`~!#$^&*()=|{}':;',\\[\\].<>/?~！#￥……&*（）——|{}【】‘；：”“'。，、？]‘’"; //Specific Key list  
//	var realkey = String.fromCharCode(keyCode);
//	var flg = false;
//	flg = (specialKey.indexOf(realkey) >= 0);
//	if(flg) {
//		// alert('请勿输入特殊字符: ' + realkey);  
//		return false;
//	}
//	return true;
//}
//function banfun(){
//	$("#password").focus(function(){
//		document.onkeypress = showKeyPress;
//	});
//}
//如果cookie不为空，不能进入该页面
function cannotEnter(){
	if(getCookie("user")!=null){
		location.href = "../index.html";
	}
}
function messageTip(message){
	$("#messagebox").remove();
	$("body").append('<span id="messagebox">'+message+'</span>');
	$("#messagebox").stop().fadeIn(500).fadeOut(2000);
}
//替换转义
function html_encode(str) {
    var s = "";
    if (str.length == 0)
        return "";
    s=str.replace(/%/g,"[p];");
    s=s.replace(/&/g,"[a];");
    s=s.replace(/\+/g,"[ad];");
    return s;
}
//替换转义
function html_decode(str) {
  var s = "";
  if (str.length == 0) {
    return "";
  }
  s = str.replace(/&lt;/g, "<");
  s = s.replace(/&gt;/g, ">");
  s=s.replace(/\[p\];/g,"%");
    s=s.replace(/\[a\];/g,"&");
    s=s.replace(/\[ad\];/g,"+");
  return s;
}
//关注
function follow(blogId){
	if(getCookie("user")==null){
		messageTip("哎呀，还没登录呢！");
		return;
	}
	var url = "/HyperIT/blog/follow";
	var parm = "userdetail_id="+thisUser.id+"&target_id="+blogId;
	$.post(url,parm,function(data){
		if(data.serviceResult){
			$("#author-info>a").eq(0).text("取关").addClass("active").attr("onclick","cancelFollow(authorId)");
			messageTip(data.resultInfo);
		}else{
			messageTip(data.resultInfo);
		}
	});
}
//取关
function cancelFollow(blogId){
	if(getCookie("user")==null){
		messageTip("哎呀，还没登录呢！");
		return;
	}
	var url = "/HyperIT/blog/deleteFollow";
	var parm = "userdetail_id="+thisUser.id+"&target_id="+blogId;
	$.post(url,parm,function(data){
		if(data.serviceResult){
			$("#author-info>a").eq(0).text("关注").removeClass("active").attr("onclick","follow(authorId)");
			messageTip(data.resultInfo);
		}else{
			messageTip(data.resultInfo);
		}
	});
}
//收藏
function collect(goalId,type){
	if(getCookie("user")==null){
		messageTip("哎呀，还没登录呢！");
		return;
	}
	var url = "/HyperIT/blog/collect";
	var parm = "userdetail_id="+thisUser.id+"&id="+goalId+"&type="+type;
	$.post(url,parm,function(data){
		if(data.serviceResult){
			messageTip(data.resultInfo);
			$(".action a").eq(0).text("取消收藏").addClass("active").attr("onclick","cancelCollect(articleId,0)");//这句话放在特定的方法内
			$("#collecting").text(Number($("#collecting").text())+1);
		}else{
			messageTip(data.resultInfo);
		}
	});
}
//取消收藏
function cancelCollect(goalId,type){
	if(getCookie("user")==null){
		messageTip("哎呀，还没登录呢！");
		return;
	}
	var url = "/HyperIT/blog/deleteCollect";
	var parm = "userdetail_id="+thisUser.id+"&id="+goalId+"&type="+type;
	$.post(url,parm,function(data){
		if(data.serviceResult){
			$(".action a").eq(0).text("收藏").removeClass("active").attr("onclick","collect(articleId,0)");//这句话放在特定的方法内
			$("#collecting").text($("#collecting").text()-1);
			messageTip(data.resultInfo);
		}else{
			messageTip(data.resultInfo);
		}
	});
}
//打赏
function reward(articleId){
	if(getCookie("user")==null){
		messageTip("哎呀，还没登录呢！");
		return;
	}
	var url = "/HyperIT/blog/reward";
	var parm = "userdetail_id="+thisUser.id+"&target_id="+articleId;
	$.post(url,parm,function(data){
		if(data.serviceResult){
			messageTip(data.resultInfo);
			$("#reward").text(Number($("#reward").text())+1);
		}else{
			messageTip(data.resultInfo);
		}
	});
}
//显示私信的遮罩
function toSixin(blogId){
	if(getCookie("user")==null){
		messageTip("哎呀，还没登录呢！");
		return;
	}
	$("body").append('<div id="mask"></div>');
	$("#mask").append('<div class="mask-box sixinbox" id="sixinbox">'+
							'<h4>私信</h4>'+
							'<textarea name="sixincontent" id="sixincontent"></textarea>'+
							'<input type="button" name="submit" id="submit" value="发送" onclick="sendSixin('+blogId+')"/>'+
							'<input type="button" name="cancel" id="cancel" value="取消" onclick="cancelSixin()"/>'+
						'</div>');
	$("#mask").show();
}
//私信ajax
function sendSixin(blogId){
	if(getCookie("user")==null){
		messageTip("哎呀，还没登录呢！");
		return;
	}
	var url = "/HyperIT/blog/comment";
	var parm = "userDetail_id="+thisUser.id+"&target_id="+blogId+"&content="+$("#sixincontent").val()+"&type=3";
	$.post(url,parm,function(data){
		if(data.serviceResult){
			$("#mask").remove();
			messageTip(data.resultInfo);
		}else{
			messageTip(data.resultInfo);
		}
	});
}
//关闭私信遮罩
function cancelSixin(){
	$("#mask").remove();
}
//显示遮罩，并初始化举报div
function toReport(goalId,type){
	if(getCookie("user")==null){
		messageTip("哎呀，还没登录呢！");
		return;
	}
	$("body").append('<div id="mask"></div>');
	$("#mask").append('<div class="mask-box reportbox" id="reportbox">'+
							'<h4>举报</h4>'+
							'<input type="radio" name="reportRadio" value="传播色情、暴力、邪教等不良信息。" checked/>'+
							'<span>传播色情、暴力、邪教等不良信息。</span>'+
							'<input type="radio" name="reportRadio" value="文章未经允许转发。" />'+
							'<span>文章未经允许转发。</span>'+
							'<input type="radio" name="reportRadio" value="内容存在攻击性语言。" />'+
							'<span>内容存在攻击性语言。</span>'+
							'<input type="radio" name="reportRadio" value="抄袭嫌疑，非原创。" />'+
							'<span>抄袭嫌疑，非原创。</span>'+
							'<textarea name="reportcontent" id="reportcontent" placeholder="在此可以补充说明，提交相关证据，举报被采纳率更高。"></textarea>'+
							'<input type="button" name="submit" id="submit" value="发送" onclick="report('+goalId+','+type+')"/>'+
							'<input type="button" name="cancel" id="cancel" value="取消" onclick="cancelReport()"/>'+
						'</div>');
	$("#mask").show();
}
//退出或者已经完成举报，关闭遮罩
function cancelReport(){
	$("#mask").remove();
}
//举报ajax
function report(goalId,type){
	if(getCookie("user")==null){
		messageTip("哎呀，还没登录呢！");
		return;
	}
	var reportContent = $("input:radio:checked").val()+$("#reportcontent").val();
	var url = "/HyperIT/blog/report";
	var parm = "userDetail_id="+thisUser.id+"&goal_id="+goalId+"&type="+type+"&reason="+reportContent;
	$.post(url,parm,function(data){
		if(data.serviceResult){
			cancelReport();
			messageTip(data.resultInfo);
		}else{
			messageTip(data.resultInfo);
		}
	});
}
